<?php
namespace App\Controller;

class MallBannersController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：新增banner
   * */
    public function add()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        $mall_banner = $this->MallBanners->newEntity();
        $mall_banner->title = $data['title'];
        $mall_banner->img = empty($data['img']) ? '':$this->G->setImgUrl($data['img']);
        $mall_banner->start_date = empty($data['start_date'])? 0:strtotime($data['start_date']);
        $mall_banner->end_date = empty($data['end_date'])? 0:strtotime($data['end_date']);
        $mall_banner->is_resident = empty($data['is_resident']) ? 0:$data['is_resident'];
        $mall_banner->url = empty($data['url']) ? '':$data['url'];
        $mall_banner->order_num = empty($data['order_num']) ? 0:$data['order_num'];
        $mall_banner->enabled = $data['enabled'] == 1 ? 1:0;
        $this->MallBanners->save($mall_banner);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：修改banner
   * */
    public function update()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        $mall_banners = $this->MallBanners->findById($data['id'])->select(['id'])->first();
        if(empty($mall_banners)){
            $this->G->error("mall_banner_error");
            return null;
        }
        $mall_banner = $this->MallBanners->newEntity();
        $mall_banner->id = $data['id'];
        $mall_banner->title = $data['title'];
        $mall_banner->img = empty($data['img']) ? '':$this->G->setImgUrl($data['img']);
        $mall_banner->start_date = empty($data['start_date'])? 0:strtotime($data['start_date']);
        $mall_banner->end_date = empty($data['end_date'])? 0:strtotime($data['end_date']);
        $mall_banner->is_resident = empty($data['is_resident']) ? 0:$data['is_resident'];
        $mall_banner->url = empty($data['url']) ? '':$data['url'];
        $mall_banner->order_num = empty($data['order_num']) ? 0:$data['order_num'];
        $mall_banner->enabled = $data['enabled'] == 1 ? 1:0;
        $this->MallBanners->save($mall_banner);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：删除banner
   * */
    public function delete()
    {
        $id = $this->request->getData('id');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->MallBanners->deleteAll(['id'=>$id]);
        $this->G->success("Success");
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：banner列表
   * */
    public function getList()
    {
        $mall_banners = $this->MallBanners->find()
            ->order(['order_num desc'])
            ->toArray();
        $re = [];
        $re['list'] = [];
        if(!empty($mall_banners)){
            foreach ($mall_banners as $k=>$v){
                $mall_banners[$k]['start_date'] = empty($v['start_date']) ? '':date('Y-m-d H:i:s', $v['start_date']);
                $mall_banners[$k]['end_date'] = empty($v['end_date']) ? '':date('Y-m-d H:i:s', $v['end_date']);
                $mall_banners[$k]['img'] = empty($v['img']) ? '' : $this->G->getImgUrl($v['img']);
                $re['list'][] = $mall_banners[$k];
            }
        }
        $this->G->success("Success", $re);
    }

}
?>